package com.niit.happynicedays;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class valentineday extends Activity implements OnTouchListener{

	ImageView imBackground, imPigeon,imPig,imBird;
	MediaPlayer mMedia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.valentine);

		imBackground = (ImageView) findViewById(R.id.valentine_bg);
		imBackground.setBackgroundResource(R.drawable.valentine);

		mMedia = MediaPlayer.create(getBaseContext(), R.raw.myvalentine);
		mMedia.setLooping(true);
		mMedia.start();

		imPigeon = (ImageView) findViewById(R.id.bocau);
		imPigeon.setBackgroundResource(R.drawable.bocau1);
		setLayout(imPigeon, 200, 200, 200, 200);
		imPigeon.setOnTouchListener(this);
		
		imPig = (ImageView) findViewById(R.id.loncon);
		imPig.setBackgroundResource(R.drawable.tiny);
		setLayout(imPig, 0, 300,-200, 0);		
		imPig.setOnTouchListener(this);
		
		imBird = (ImageView) findViewById(R.id.bird);
		imBird.setBackgroundResource(R.drawable.bird1);
		setLayout(imBird,0, 200,200, 200);
		imBird.setOnTouchListener(this);
		
		mMedia.setVolume(0, 0);
	}

	private void animPigeon() {
		if (imPigeon != null) {
			imPigeon.setBackgroundResource(R.anim.pigeon);

			AnimationDrawable ani = (AnimationDrawable) imPigeon.getBackground();
			if (ani.isRunning()) {
				ani.stop();
			} else {
				ani.stop();
				ani.start();
			}
		}
		}
		private void animBird() {
			if (imBird != null) {
				imBird.setBackgroundResource(R.anim.bird);

				AnimationDrawable ani = (AnimationDrawable) imBird.getBackground();
				if (ani.isRunning()) {
					ani.stop();
				} else {
					ani.stop();
					ani.start();
				}
			}
	}
	private void animPig() {
		if (imPig != null) {
			imPig.setBackgroundResource(R.anim.pig);

			AnimationDrawable ani = (AnimationDrawable) imPig.getBackground();
			if (ani.isRunning()) {
				ani.stop();
			} else {
				ani.stop();
				ani.start();
			}
		}
	}
	
	

	private void setLayout(View v, int left, int top, int right, int bottom) {
		RelativeLayout.LayoutParams privew = new RelativeLayout.LayoutParams(
				Math.round(v.getLayoutParams().width), Math.round(v
						.getLayoutParams().height));

		privew.setMargins(left, top, right, bottom);
		v.setLayoutParams(privew);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(getBaseContext(), nicedays.class);
			startActivity(intent);
			mMedia.stop();
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bocau: {
			animPigeon();
			break;
		}
		case R.id.loncon: {
			animPig();
			break;
		}
		case R.id.bird: {
			animBird();
			break;
		}
		default:
			break;
		}
		return false;
	}

}
